#pragma once
#include <vector>
#include "nodes\DynamicSpriteNode.h"
#include "base\Controller.h"
#include "Constants.h"
#include "lib\container\GridArray.h"

class Grid : public ds::MouseController {
	
enum GridState {
	GS_NORMAL,
	GS_FLOATING,
	GS_FALLING,
	GS_IDLE
};

struct Piece {
	ds::Sprite* sprite;
	int color;
	ds::Vec2 position;
	int fallingVelocity;
};

typedef std::vector<Piece> Pieces;
typedef std::vector<Piece> ShrinkList;

public:
	Grid();
	~Grid();
	void create(int level);
	bool update(const float& elapsedTime);
	int getPoints() { 
		return points;
	}
	int getMoves() { 
		return movesLeft;
	}
	void OnButtonDown(int button);
	void OnButtonUp(int button) {}	
	void setActive(bool active);
	void clear();
private:
	// methods
	void removePiece(int x,int y);
	int checkSurrounding(int xp,int yp);
	int getMovesLeft();
	void check(int xp,int yp,int latDir,std::vector<ds::Vec2> *list,bool rec);
	int getColor(int xp,int yp);
	int getIndex(int xp,int yp);
	bool isRowEmpty(int xp);
	void moveDown();
	void shiftLeft(int xp);	
	// attributes
	Pieces m_Pieces;
	ds::DynamicSpriteNode* m_SpriteNode;
	float clickCounter;
	int points;
	int movesLeft;
	bool running;
	ShrinkList m_ShrinkList;
	GridState m_State;
	ds::GridArray<Piece> m_AllPieces;
};

